package com.derbysoft.dswitch.adapter.ihg.common.repository.impl;

import com.derbysoft.dswitch.adapter.ihg.common.domain.ReservationDetail;
import com.derbysoft.dswitch.adapter.ihg.common.domain.ReservationLog;
import com.derbysoft.dswitch.adapter.ihg.common.repository.ReservationLogRepository;
import com.derbysoft.dswitch.adapter.ihg.common.utils.CollectionUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

@Component("reservationLogRepository")
public class ReservationLogRepositoryImpl implements ReservationLogRepository {

    private static final String INSERT_SQL = "INSERT INTO reservation_log(id,channel,error,hotels,operation_type,task_id,timestamp,ersp,exception,time_span)" +
            " VALUES(?,?,?,?,?,?,?,?,?,?);";

    private static final String INSERT_LOG_DETAIL_SQL =
            "INSERT INTO reservation_detail(detail,direction,timestamp,reservation_log)  VALUES(?,?,?,?);";

    @Resource
    protected JdbcTemplate jdbcTemplate;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void save(final ReservationLog reservationLog) {
        jdbcTemplate.update(INSERT_SQL, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, reservationLog.getId());
                ps.setString(2, reservationLog.getChannel());
                ps.setBoolean(3, reservationLog.isError());
                ps.setString(4, reservationLog.getHotels());
                ps.setString(5, reservationLog.getOperationType().name());
                ps.setString(6, reservationLog.getTaskId());
                ps.setTimestamp(7, new Timestamp(reservationLog.getTimestamp().getTime()));
                ps.setString(8, reservationLog.getErsp());
                ps.setString(9, reservationLog.getException());
                ps.setLong(10, reservationLog.getTimeSpan());
            }
        });

        if (CollectionUtil.isNotEmpty(reservationLog.getReservationDetails())) {
            for (final ReservationDetail reservationDetail : reservationLog.getReservationDetails()) {
                jdbcTemplate.update(INSERT_LOG_DETAIL_SQL, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement ps) throws SQLException {
                        ps.setString(1, reservationDetail.getDetail());
                        ps.setString(2, reservationDetail.getDirection().name());
                        ps.setTimestamp(3, new Timestamp(reservationDetail.getTimestamp().getTime()));
                        ps.setString(4, reservationLog.getId());
                    }
                });
            }
        }
    }
}
